package com.pignest.hotel.ui.reception;

import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.event.PagingEvent;

import com.pignest.hotel.beans.Booking;
import com.pignest.hotel.beans.BookingRoom;
import com.pignest.hotel.beans.Member;
import com.pignest.hotel.definition.BookingStatus;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.MoneyUtil;

public class LivingBookingListbox extends Listbox {

	/**
	 * 预定查询
	 */
	private static final long serialVersionUID = -1213611597553332543L;
	

	private Paging page;
	private ReceptionService receptionService;
	private String name;
	private String mobile;
	

	private Checkbox isBookingCkb; //是否有预定
	private Label bookingRooms;
	private Listbox LivingMemListbox;
	
   public LivingBookingListbox(){
	   receptionService=Services.getReceptionService();
   }
   public void onCreate() {

	   isBookingCkb =(Checkbox)getFellow("isBookingCkb");
	   bookingRooms=(Label)getFellow("bookingRooms");
	   LivingMemListbox=(Listbox)getFellow("LivingMemListbox");
	   page =  (Paging)getFellow("pagBooking");
	   final int PAGE_SIZE = page.getPageSize();
	   page.addEventListener("onPaging", new EventListener() {
		      public void onEvent(Event event) {
		        	PagingEvent pe = (PagingEvent) event;
		        	int pgno = pe.getActivePage();
		        	int start = pgno*PAGE_SIZE;
		        	queryBooking(start,PAGE_SIZE);
		      	}
		    	});
   }
   public void queryBooking(int start,int pageSize){
	   this.getItems().clear();
	   StringBuffer hql=new StringBuffer();
	   hql.append(" from Booking b where 1=1");
	   if(!name.equals(""))
		   hql.append(" and b.name like '%").append(name).append("%'");
	   if(!mobile.equals(""))
		   hql.append(" and b.phone like '%").append(mobile).append("%'");
	   List l =(List)receptionService.find(hql.toString());
	   int Total =l.size();
	   List<Booking> bookings=receptionService.queryPageHql(hql.toString(), start, pageSize);
	  // List<Booking> bookings=receptionService.listAllBookings();
	   // List<Booking> bookings=receptionService.queryBooking(name, mobile, null);
	   page.setTotalSize(Total);
	   for(final Booking b:bookings){
			
			Listitem listitem = new Listitem();
			listitem.setValue(b.getId());
			listitem.setParent(this);
			listitem.appendChild(new Listcell(b.getBookingStatus()));
			listitem.appendChild(new Listcell(b.getName()));
			listitem.appendChild(new Listcell(b.getPhone()));
			listitem.appendChild(new Listcell(DateUtil.toFullString(b.getEstimatedTime())));
			//listitem.appendChild(new Listcell(getBookingRoomes(b.getId())));
			listitem.appendChild(new Listcell(String.valueOf(b.getPreserve()/60)));
			listitem.appendChild(new Listcell(DateUtil.toFullString(b.getEndTime())));
			listitem.appendChild(new Listcell((b.getLeaveTimeLabel())));
			
			listitem.addEventListener(Events.ON_CLICK,  new EventListener(){
				public void onEvent(Event event) throws Exception { 
					//this.selectRoom=r;
					String sBookingRooms=getBookingRoomes(b.getId());
					bookingRooms.setValue(sBookingRooms);
					isBookingCkb.setChecked(true);
					
				}
			});
		}
   }
public String getBookingRoomes(String bookinkId){
	StringBuffer hql = new StringBuffer();
	if(bookinkId==null)
		return "未排房";

		hql.append(" from BookingRoom b where b.booking.id='"+bookinkId+"'");
		List<BookingRoom> rooms= receptionService.find(hql.toString());
		boolean b = true;
		StringBuilder sb = new StringBuilder();
		for(BookingRoom r:rooms){
			if (!b) {
				sb.append(',');
			} else {
				b = false;
			}
			sb.append(r.getRoom().getName());
		}
	
	return sb.toString();
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getMobile() {
	return mobile;
}
public void setMobile(String mobile) {
	this.mobile = mobile;
}
   
}
